﻿using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace YandexWebmaster
{
    public partial class API
    {
        public async Task<List<Site>> GetSiteAPI(Account account)
        {
            List<Site> arSite = new List<Site>();
            // адрес сервера
            string serverAddress = $"https://api.webmaster.yandex.net/v4/user/{account.User.ID}/hosts";
            // устанавливаем заголовок
            httpClient.DefaultRequestHeaders.Add("Authorization", "OAuth " + account.OauthToken);

            using (var response = await httpClient.GetAsync(serverAddress).ConfigureAwait(false))
            {
                string content = await response.Content.ReadAsStringAsync();
                dynamic result = JsonConvert.DeserializeObject(content);
                foreach (var item in result["hosts"])
                {
                    Site site = new Site
                    {
                        ID_Account = account.ID,
                        HostID = item.host_id,
                        Verified = item.verified,
                        Domain = item.ascii_host_url,
                        DomainUnicode = item.unicode_host_url,
                        InWebmaster = true,
                    };
                    arSite.Add(site);
                }
            }
            return arSite;
        }
    }
}
